//
//  WXPComplainVC.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/7.
//
//

#import "WXPComplainVC.h"
#import "ServiceItemView.h"

@interface WXPComplainVC ()

@property (nonatomic, strong) WXPNavView * navView;
@property (nonatomic, strong) WXPCommonAgreementView * agreementView;
@property (nonatomic, strong) UIView * headerView;
@property (nonatomic, strong) ServiceItemView * phoneItemView;
@property (nonatomic,   copy) NSString * phoneNum;
@end

@implementation WXPComplainVC

#pragma mark - lazy
- (WXPNavView *)navView{
    if (_navView == nil) {
        _navView = [WXPNavView  new];
        [_navView resetViewWithTitle:@"Línea de reclamación"];
        _navView.btnService.hidden = true;
    }
    return  _navView;
}
- (WXPCommonAgreementView *)agreementView{
    if (!_agreementView) {
        _agreementView = [WXPCommonAgreementView new];
        _agreementView.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-LEFT_MARDIN);
    }
    return _agreementView;
}
- (UIView *)headerView{
    if (_headerView == nil) {
        _headerView = [UIView  new];
        _headerView.userInteractionEnabled = true;
        _headerView.width = SCREEN_WIDTH-2*LEFT_MARDIN;
    }
    return  _headerView;
}
- (ServiceItemView *)phoneItemView{
    if (_phoneItemView == nil) {
        _phoneItemView = [ServiceItemView  new];
        WEAKSELF
        _phoneItemView.blockItemClick = ^{
            [GlobalMethod hitPointWithPageName:KEY_Complaint action:KEY_Click extInfo:nil];
            NSString *phoneNumber = weakSelf.phoneNum;
            NSCharacterSet *allowedCharacters = [NSCharacterSet URLQueryAllowedCharacterSet];
            phoneNumber = [phoneNumber stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
            NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]];
            if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
                [[UIApplication sharedApplication] openURL:phoneURL options:@{} completionHandler:nil];
            } else {
                NSLog(@"设备不支持拨号功能");
            }
        };
    }
    return  _phoneItemView;
}

#pragma mark - viewDidLoad
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    // 打点
    [GlobalMethod hitPointWithPageName:KEY_Complaint action:KEY_Enter extInfo:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self requestList];
}

- (void)configUI{
    [self.view addSubview:self.agreementView];
    self.viewBG.backgroundColor = COLOR_BACKGROUND;
    self.tableView.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:self.navView];
    self.tableView.width = SCREEN_WIDTH-2*LEFT_MARDIN;
    self.tableView.centerX = SCREEN_WIDTH/2;
    self.tableView.height = self.agreementView.top-10-self.navView.bottom-10;
    self.tableView.top = self.navView.bottom+10;
}

#pragma mark - resetHeaderView
- (void)resetHeaderView{
    self.headerView = [UIView initWithViews:@[
        self.phoneItemView,
    ]];
    self.headerView.backgroundColor = COLOR_WHITE;
    [GlobalMethod setRoundView:self.headerView color:[UIColor clearColor] numRound:16 width:0];
    self.tableView.tableHeaderView = self.headerView;
}

#pragma mark - request
- (void)requestList{
    [RequestApi requestComplaintPhoneWithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        self.phoneNum = [NSString stringWithFormat:@"%@",response];
        // 电话
        ModelBtn * phoneModel = [ModelBtn new];
        phoneModel.content = self.phoneNum;
        phoneModel.title = @"Línea de reclamación";
        phoneModel.imageName = @"icon-complain-phone";
        phoneModel.highImageName = @"icon-service-phone-tail";
        [self.phoneItemView resetViewWithModel:phoneModel];
        self.phoneItemView.lineView.hidden = true;
        
        [self resetHeaderView];
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

@end
